home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / libg_261.zip / libg_261 / libg++ / old-stream / istream.h < prev    next >
C/C++ Source or Header  |  1992-01-17  |  6KB  |  255 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1989, 1992 Free Software Foundation
  4.     written by Doug Lea (dl@rocky.oswego.edu)
  5.  
  6. This file is part of the GNU C++ Library.  This library is free
  7. software; you can redistribute it and/or modify it under the terms of
  8. the GNU Library General Public License as published by the Free
  9. Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.  This library is distributed in the hope
  11. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  12. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  13. PURPOSE.  See the GNU Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with this library; if not, write to the Free Software
  16. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18.  
  19. /* *** Version 1.2 -- nearly 100% AT&T 1.2 compatible *** */
  20.  
  21. /* istream.h now separately includable */
  22.  
  23. #ifndef _istream_h
  24. #ifdef __GNUG__
  25. #pragma interface
  26. #endif
  27. #define _istream_h 1
  28.  
  29.  
  30. #include <File.h>
  31. #include <streambuf.h>
  32. #include <filebuf.h>
  33. #include <Filebuf.h>
  34.  
  35. class whitespace                // a class used only to input and
  36. {                               // discard white space characters
  37.   char filler;                     
  38. };
  39.  
  40. class ostream;
  41.  
  42. class istream
  43. {
  44.   friend void   eatwhite(istream& s);
  45. protected:
  46.   streambuf*    bp;
  47.   state_value   state;           // _good/_eof/_fail/_bad
  48.   ostream*      tied_to;
  49.   char          skipws;
  50.   char          ownbuf;
  51.   void          _flush();
  52.   char*         readline (int chunk_number, char terminator);
  53.   
  54. public:
  55.                 istream(const char* filename, io_mode m, access_mode a, 
  56.                         int sk=1, ostream* t = 0);
  57.                 istream(const char* filename, const char* m, 
  58.                         int sk=1, ostream* t = 0);
  59.                 istream(int filedesc, io_mode m, int sk=1, ostream* t = 0);
  60.                 istream(FILE* fileptr, int sk=1, ostream* t = 0);
  61.                 istream(int sz, char* buf, int sk=1, ostream* t = 0);
  62.                 istream(int filedesc, int sk=1, ostream* t = 0);
  63.                 istream(int filedesc, char* buf, int buflen, 
  64.                         int sk, ostream* t = 0);
  65.                 istream(streambuf* s, int sk=1, ostream* t = 0);
  66.  
  67.                ~istream();
  68.  
  69.   istream&      open(const char* filename, io_mode m, access_mode a);
  70.   istream&      open(const char* filename, const char* m);
  71.   istream&      open(int  filedesc, io_mode m);
  72.   istream&      open(FILE* fileptr);
  73.   istream&      open(const char* filenam, open_mode m);
  74.  
  75.   istream&      close();
  76.  
  77.   ostream*      tie(ostream* s);
  78.   int           skip(int);
  79.  
  80. // stream status
  81.  
  82.   int           rdstate();
  83.   int           eof();
  84.   int           fail();
  85.   int           bad();
  86.   int           good();
  87.  
  88. // other status queries
  89.  
  90.   int           readable();
  91.   int           writable();
  92.   int           is_open();
  93.  
  94.                 operator void*();
  95.   int           operator !();
  96.  
  97.   const char*   name();
  98.  
  99.   char*         bufptr();
  100.  
  101. // error handling
  102.  
  103.   void          error();
  104.   void          clear(state_value f = _good); // poorly named
  105.   void          set(state_value f); // set corresponding bit
  106.   void          unset(state_value f); // clear corresponding bit
  107.   istream&      failif(int cond);
  108.  
  109. // unformatted IO
  110.  
  111.   istream&      get(char& c);
  112.   istream&      unget(char c);
  113.   istream&      putback(char c); // a synonym for unget
  114.  
  115.   istream&      get    (char* s, int n, char terminator = '\n');
  116.   istream&      getline(char* s, int n, char terminator = '\n');
  117.   istream&      gets   (char **s, char terminator = '\n');
  118.  
  119.  
  120.   istream&      operator >> (char&   c);
  121.   istream&      operator >> (short&  n);
  122.   istream&      operator >> (unsigned short& n);
  123.   istream&      operator >> (int&    n);
  124.   istream&      operator >> (unsigned int& n);
  125.   istream&      operator >> (long&   n);
  126.   istream&      operator >> (unsigned long& n);
  127. #ifdef __GNUG__
  128.   istream&      operator >> (long long& n);
  129.   istream&      operator >> (unsigned long long& n);
  130. #endif
  131.   istream&      operator >> (float&  n);
  132.   istream&      operator >> (double& n);
  133.   istream&      operator >> (char*   s);
  134.   istream&      operator >> (whitespace& w);
  135. };
  136.  
  137. // pre-declared streams
  138.  
  139. extern istream  cin;             // stdin
  140.  
  141. extern whitespace WS;            // for convenience (Old name)
  142. extern whitespace ws;            // for convenience (iostream-style name)
  143.  
  144. #if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
  145.  
  146.  
  147. inline void istream::clear(state_value flag)
  148. {
  149.   state = flag;
  150. }
  151.  
  152. inline void istream::set(state_value flag)
  153. {
  154.   state = state_value(int(state) | int(flag));
  155. }
  156.  
  157. inline void istream::unset(state_value flag)
  158. {
  159.   state = state_value(int(state) & ~int(flag));
  160. }
  161.  
  162. inline int istream::rdstate()
  163. {
  164.   return int(state);
  165. }
  166.  
  167. inline int istream::good()
  168. {
  169.   return state == _good;
  170. }
  171.  
  172. inline int istream::eof()
  173. {
  174.   return int(state) & int(_eof);
  175. }
  176.  
  177. inline int istream::fail()
  178. {
  179.   return int(state) & int(_fail);
  180. }
  181.  
  182. inline int istream::bad()
  183. {
  184.   return int(state) & int(_bad);
  185. }
  186.  
  187. inline istream::operator void*()
  188. {
  189.   return (state == _good)? this : 0;
  190. }
  191.  
  192. inline int istream::operator !()
  193. {
  194.   return (state != _good);
  195. }
  196.  
  197. inline istream& istream::failif(int cond)
  198. {
  199.   if (cond) set(_fail); return *this;
  200. }
  201.  
  202. inline int istream::is_open()
  203. {
  204.   return bp->is_open();
  205. }
  206.  
  207. inline int istream::readable()
  208. {
  209.   return (bp != 0) && (bp->is_open()) && (state == _good);
  210. }
  211.  
  212. inline int istream::writable()
  213. {
  214.   return 0;
  215. }
  216.  
  217.  
  218. inline char* istream::bufptr()
  219. {
  220.   return bp->base;
  221. }
  222.  
  223.  
  224. inline istream& istream::close()
  225. {
  226.   bp->close();  return *this;
  227. }
  228.  
  229.  
  230. inline int istream::skip(int sk)
  231. {
  232.   int was = skipws; skipws = sk; return was;
  233. }
  234.  
  235.  
  236. inline istream& istream::unget(char c)
  237. {
  238.   if (bp->sputbackc(c) == EOF) set(_fail); return *this;
  239. }
  240.  
  241. inline istream& istream::putback(char c)
  242. {
  243.   if (bp->sputbackc(c) == EOF) set(_fail); return *this;
  244. }
  245.  
  246. inline void eatwhite(istream& s)
  247. {
  248.   s >> WS;
  249. }
  250.  
  251. #endif
  252.  
  253.  
  254. #endif
  255.